<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:s="http://jboss.org/schema/seam/taglib"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:rich="http://richfaces.org/rich"
  xmlns:zanata="http://java.sun.com/jsf/composite/zanata"
  template="../WEB-INF/template/template.xhtml">

<ui:define name="page_title">
  #{msgs['jsf.Version']} -
  #{versionHomeAction.projectSlug}:#{versionHomeAction.versionSlug}
</ui:define>

<ui:define name="center_content">
<script type="text/javascript">
  //<![CDATA[
  jQuery(function () {
    crossroads.addRoute(':pre*:/iteration/view/{projectSlug}/{versionSlug}/languages/:lang:',
      function (pre, projectSlug, versionSlug, lang) {
        zanata.tabs.activate(jQuery('#languages_tab'));
        var clickEle = jQuery('a.' + lang);
        if (lang && clickEle.length != 0) {
          updateActiveRow(jQuery('a.' + lang))
          toggleColumn('languages')
          reloadLocale(lang)
        }
      });

    crossroads.addRoute(':pre*:/iteration/view/{projectSlug}/{versionSlug}/documents/:docId*:',
      function (pre, projectSlug, versionSlug, docId) {
        zanata.tabs.activate(jQuery('#documents_tab'));
        var clickEle = jQuery('#' + jqSelector(docId)).children().eq(1);
        if (docId && clickEle.length != 0) {
          updateActiveRow(clickEle);
          toggleColumn('documents');
          reloadDocument(projectSlug, versionSlug, docId)
        }
      });

    crossroads.addRoute(':pre*:/iteration/view/{projectSlug}/{versionSlug}/settings/:setting:',
      function (pre, projectSlug, versionSlug, setting) {
        zanata.tabs.activate(jQuery('#settings_tab'));
        setting = setting || 'general';
        setting =
          validateTab("#settings-" + setting + '_tab', setting, 'general');
        zanata.tabs.activate(jQuery('#settings-' + setting + "_tab"))
      });


    crossroads.addRoute(':pre*:/iteration/view/{projectSlug}/{versionSlug}/:section:',
      function (pre, projectSlug, versionSlug, section) {
        section = section || 'languages'
        section = validateTab('#' + section + "_tab", section, "languages");
        zanata.tabs.activate(jQuery('#' + section + "_tab"))
      });

    renderStatistics();
  })
  //]]>


  function openUploadSourceDocPanel() {
    #{rich:component('uploadSourceDocPanel')}
  .
    show();
    checkIfGeneralFileSelected()
  }

  function openUploadSourcePanel(docId) {
    #{rich:element('uploadSourcePanelHeader')}
  .
    innerHTML =
      '#{msgs['jsf.iteration.files.UpdateDocument']}: ' + docId;
    #{rich:element('uploadSourceDocId')}
  .
    value = docId;
    #{rich:component('uploadSourcePanel')}
  .
    show();
    checkIfSpecificFileSelected();
  }

  function hideUploadSourcePanel() {
    #{rich:component('uploadSourcePanel')}
  .
    hide();
    return false;
  }

  function hideSourceDocUploadPanel() {
    #{rich:component('uploadSourceDocPanel')}
  .
    hide();
    return false;
  }

  function checkIfGeneralFileSelected() {
    var fileInput = #{rich:element('generalDocFileUpload')};
    var submitButton = #{rich:element('generalDocSubmitUploadButton')};
    var errorMessage = document.getElementById('invalidFilenameError');
    checkIfFileSelected(fileInput, submitButton);
    var fileName = fileInput.value;
    var hasSemicolon = fileName !== undefined &amp;&amp;
      fileName.lastIndexOf(';') !== -1;
    if (hasSemicolon) {
      errorMessage.style.display = '';
      submitButton.disabled = true;
    } else {
      errorMessage.style.display = 'none';
    }
  }

  function checkIfSpecificFileSelected() {
    checkIfFileSelected(#{rich:element('specificDocFileInput')},
      #{rich:element('specificDocSubmitUploadButton')});
  }

  function checkIfFileSelected(fileInput, submitButton) {
    var noFileSelected = fileInput.value === undefined ||
      fileInput.value === "";
    submitButton.disabled = noFileSelected;
  }
</script>

<a4j:jsFunction name="reloadLocale" status="languageTab-documentsLoader"
  onbegin="clearHTML('#{rich:clientId('languages-document_list')}');clearHTML('#{rich:clientId('language-label')}');zanata.loader.activate('#languagesTab-language-label-loader')"
  oncomplete="zanata.loader.deactivate('#languagesTab-language-label-loader')"
  render="languages-document_list, language-label, languages-export_options, languageTabDocumentSearch-pager, languageTabDocumentSearch-page-info, languageTabDocumentSearchBottom-pager, languageTabDocumentSearchBottom-page-info">
  <a4j:param name="val1" assignTo="#{versionHomeAction.selectedLocaleId}"/>
</a4j:jsFunction>

<a4j:jsFunction name="reloadDocument" status="documentsTab-languagesLoader"
  onbegin="clearHTML('#{rich:clientId('documents-language_list')}');clearHTML('#{rich:clientId('document-label')}');zanata.loader.activate('#documentsTab-document-label-loader')"
  oncomplete="zanata.loader.deactivate('#documentsTab-document-label-loader')"
  render="documents-language_list, document-label, documents-language-size, documentsTabLanguageSearch-pager, documentsTabLanguageSearch-page-info, documentsTabLanguageSearchBottom-pager, documentsTabLanguageSearchBottom-page-info"
  action="#{versionHomeAction.setSelectedDocumentId(projSlug, verSlug, docId)}">
  <a4j:param name="val1" assignTo="#{projSlug}"/>
  <a4j:param name="val2" assignTo="#{verSlug}"/>
  <a4j:param name="val3" assignTo="#{docId}"/>
</a4j:jsFunction>

<a4j:jsFunction name="refreshStatistics" status="statistic-loader"
  oncomplete="refreshTooltip('version-page')" limitRender="true"
  render="overall_statistic, languages-language_form, languages-document_list, documents-document_form, documents-language_list, settings-document_form, languages-size, documents-size, language-label, document-label"
  action="#{versionHomeAction.resetPageData}"/>

<a4j:jsFunction name="renderStatistics" status="statistic-loader"
  oncomplete="refreshTooltip('version-page');updateStateFromUrl()"
  render="overall_statistic, languages-language_form, documents-document_form, settings-document_form"
  action="#{versionHomeAction.setPageRendered(true)}"/>

<a4j:jsFunction name="runCopyTrans" action="#{copyTransAction.startCopyTrans}"
  render="copy-trans, copy-trans-buttons"/>

<a4j:jsFunction name="cancelCopyTrans" action="#{copyTransAction.cancel}"
  render="copy-trans, copy-trans-buttons"/>


<div class="g" id="version-page">
<div class="g__item w--1-m w--3-8-l w--1-3 l--push-bottom-half">
  <p class="txt--meta l--push-all-0">
    <s:link view="/project/home.xhtml" propagation="none"
      value="#{msgs['jsf.Projects']}"/>
    <i class="i i--arrow-right"></i>
    <s:link
      value="#{versionHomeAction.version.project.name}" propagation="none"
      view="/project/project.xhtml">
      <f:param name="slug" value="#{versionHomeAction.projectSlug}"/>
    </s:link>
  </p>

  <s:div styleClass="l--push-bottom-half" id="version-info">
    <s:fragment rendered="#{versionHome.instance.status eq 'ACTIVE'}">
      <h1 class="l--push-all-0">
        <i class="i--small i--version txt--neutral i--left"></i>
        #{versionHomeAction.versionSlug}
      </h1>
    </s:fragment>
    <s:fragment rendered="#{versionHome.instance.status ne 'ACTIVE'}">
      <h1 class="l--push-all-0 txt--neutral">
        <i class="i--small i--version txt--neutral i--left"></i>
        #{versionHomeAction.versionSlug}
        <i class="i--small i--right i--lock"
          title="#{msgs['jsf.iteration.tooltip.readonly']}"></i>
      </h1>
    </s:fragment>
  </s:div>

  <s:div styleClass="progress-bar--large__expander l--pad-bottom-half"
    id="overall_statistic">
    <s:fragment rendered="#{versionHomeAction.pageRendered}">
      <p class="d--top l--pad-top-half txt--mini">
        #{msgs['jsf.TotalSourceContains']}
        <strong>#{versionHomeAction.overallStatistic.total} words</strong>
      </p>
      <ul class="g--tight g--fit">
        <s:fragment
          rendered="#{versionHome.instance.requireTranslationReview}">
          <li class="g__item l--push-bottom-half">
              <span class="stats txt--state-highlight">
                <span class="stats__figure">
                  #{statisticBar.formatPercentage(versionHomeAction.overallStatistic.percentApproved)}%
                </span>
                <span
                  class="stats__unit txt--lowercase"> #{msgs['jsf.stats.Approved']}</span>
              </span>
          </li>
        </s:fragment>

        <li class="g__item l--push-bottom-half">
              <span
                class="txt--state-success #{versionHome.instance.requireTranslationReview ? 'stats' : 'stats--large'}">
                <span class="stats__figure">
                  #{statisticBar.formatPercentage(versionHomeAction.overallStatistic.percentTranslated)}%
                </span>
                <span
                  class="stats__unit txt--lowercase"> #{msgs['jsf.stats.Translated']}</span>
              </span>
        </li>
        <li class="g__item l--push-bottom-half">
              <span
                class="#{versionHome.instance.requireTranslationReview ? 'stats' : 'stats--large'}">
                <span class="stats__figure">
                  #{statisticBar.formatHours(versionHomeAction.overallStatistic.remainingHours)}
                </span>
                <span class="stats__unit txt--lowercase">
                  #{msgs['jsf.stats.TotalHoursRemaining']}
                </span>
              </span>
        </li>
      </ul>
      <zanata:statistic
        value="#{versionHomeAction.overallStatistic}"
        type="large"/>
    </s:fragment>

    <zanata:loader type="loader--large"
      rendered="#{!versionHomeAction.pageRendered}"/>
  </s:div>

  <s:div id="copy-trans"
    styleClass="panel l--push-top-1 #{copyTransAction.inProgress ? '' : 'is-hidden'}">
    <s:div styleClass="panel__header" rendered="#{copyTransAction.inProgress}">
      <s:div
        styleClass="panel__header__actions l--push-right-half l--push-bottom-0"
        rendered="#{s:hasPermission(versionHomeAction.version, 'update')}">

        <a href="javascript:void(0)" onclick="cancelCopyTrans()"
          title="#{msgs['jsf.CopyTrans.cancel']}"
          class="button--small button--link">#{msgs['jsf.Cancel']}</a>

      </s:div>

      <h2 class="panel__heading--small">#{msgs['jsf.CopyTrans']}</h2>
    </s:div>

    <div class="l--pad-all-half l--push-top-quarter">
      <zanata:progressBar id="copy-trans-progress-bar"
        actionBean="#{copyTransAction}" interval="5000"
        render="copy-trans-progress-messages" oncomplete="refreshStatistics()"
        onCompleteRender="copy-trans-buttons, copy-trans"/>

      <s:div id="copy-trans-progress-messages"
        styleClass="txt--meta l--push-top-quarter">
        <zanata:loader layout="inline" type="loader--small"
          rendered="#{copyTransAction.inProgress}"/>
        <span>
          <h:outputText
            value="#{msgs.format('jsf.iteration.CopyTrans.processedItems',
              copyTransAction.currentProgress, copyTransAction.maxProgress)}"/>
          ,
          <h:outputText
            value="#{msgs.format(
              'jsf.iteration.CopyTrans.estimatedTimeRemaining',
              copyTransAction.copyTransEstimatedTimeLeft)}"/>
        </span>
      </s:div>
    </div>
  </s:div>
</div>

<div class="g__item w--1-m w--5-8-l w--2-3">
  <div class="tabs--lined js-tabs">
    <ul class="tabs__nav js-tabs-nav">
      <li>
        <a id="languages_tab"
          href="#{request.contextPath}/iteration/view/#{versionHomeAction.projectSlug}/#{versionHomeAction.versionSlug}/languages"
          class="js-url-mod" data-content="#languages">
          <i class="i i--language"></i>
          <span class="is-hidden--s i__text--right">
            #{msgs['jsf.Languages']}
            <s:span styleClass="txt--neutral" id="languages-size">
              #{versionHomeAction.getSupportedLocale().size}
            </s:span>
          </span>
        </a>
      </li>
      <li>
        <a id="documents_tab"
          href="#{request.contextPath}/iteration/view/#{versionHomeAction.projectSlug}/#{versionHomeAction.versionSlug}/documents"
          class="js-url-mod" data-content="#documents">
          <i class="i i--document"></i>
                <span class="is-hidden--s i__text--right">
                  #{msgs['jsf.Documents']}
                  <s:span styleClass="txt--neutral"
                    id="documents-size">
                    #{versionHomeAction.getDocuments().size}
                  </s:span>
                </span>
        </a>
      </li>
      <s:fragment rendered="#{not empty versionHomeAction.groups}">
        <li>
          <a id="groups_tab"
            href="#{request.contextPath}/iteration/view/#{versionHomeAction.projectSlug}/#{versionHomeAction.versionSlug}/groups"
            class="js-url-mod" data-content="#groups">
            <i class="i i--group"></i>
                <span class="is-hidden--s i__text--right">
                  #{msgs['jsf.Groups']}
                  <s:span styleClass="txt--neutral" id="groups-size">
                    #{versionHomeAction.groups.size}
                  </s:span>
                </span>
          </a>
        </li>
      </s:fragment>
      <s:fragment rendered="#{identity.loggedIn}">
        <li class="tab__end">
          <div
            class="dropdown dropdown--tab dropdown--small dropdown--right dropdown--inline js-dropdown">
            <a class="dropdown__toggle js-dropdown__toggle"
              title="#{msgs['jsf.tooltip.MoreActions']}" href="#">
              <i class="i i--ellipsis"></i>
            </a>
            <ul class="dropdown__content js-dropdown__content"
              role="content" aria-labelledby="dropdownContent">
              <li>
                <h:outputLink id="exportTMXIterLink"
                  onclick="return confirm('#{msgs['jsf.ConfirmExportTMXIter']}')"
                  styleClass="i__item--right"
                  value="#{request.contextPath}/rest/tm/projects/#{versionHomeAction.projectSlug}/iterations/#{versionHomeAction.versionSlug}">
                  #{msgs['jsf.iteration.ExportTMXIter']}<i
                  class="i i--export i__item__icon"></i>
                </h:outputLink>
              </li>
              <li>
                <s:link title="#{msgs['jsf.GenerateProjectConfig']}"
                  styleClass="i__item--right"
                  action="#{configurationAction.downloadGeneralConfig()}">
                  <f:param name="projectSlug"
                    value="#{versionHomeAction.projectSlug}"/>
                  <f:param name="iterationSlug"
                    value="#{versionHomeAction.versionSlug}"/>
                  #{msgs['jsf.ConfigFile']}
                  <i class="i i--export i__item__icon"></i>
                </s:link>
              </li>
              <s:fragment
                rendered="#{s:hasPermission(versionHomeAction.version, 'update')}">
                <li>
                  <a href="javascript:void(0)" class="i__item--right"
                    onclick="zanata.modal.show('#copy-trans-modal')">
                    #{msgs['jsf.CopyTrans']}
                    <i class="i i--translate i__item__icon"></i>
                  </a>
                </li>
              </s:fragment>
            </ul>
          </div>
        </li>
      </s:fragment>
      <s:fragment
        rendered="#{s:hasPermission(versionHomeAction.version, 'update')}">
        <li class="tab__end">
          <a id="settings_tab"
            title="#{msgs['jsf.iteration.tooltip.VersionSettings']}"
            href="#{request.contextPath}/iteration/view/#{versionHomeAction.projectSlug}/#{versionHomeAction.versionSlug}/settings"
            class="js-url-mod" data-content="#settings">
            <span class="is-hidden--s i__text--left">
              #{msgs['jsf.Settings']}
            </span>
            <i class="i i--settings"></i>
          </a>
        </li>
      </s:fragment>
    </ul>

    <ul class="tabs__content panels__container js-tabs-content l--push-top-1">
      <li id="languages">
        <ui:include
          src="../WEB-INF/layout/version/languages-tab.xhtml"/>
      </li>

      <li id="documents">
        <ui:include
          src="../WEB-INF/layout/version/documents-tab.xhtml"/>
      </li>

      <s:fragment rendered="#{!versionHomeAction.groups.isEmpty()}">
        <li id="groups">
          <ui:include
            src="../WEB-INF/layout/version/groups-tab.xhtml"/>
        </li>
      </s:fragment>

      <s:fragment
        rendered="#{s:hasPermission(versionHomeAction.version, 'update')}">
        <li id="settings">
          <ui:include
            src="../WEB-INF/layout/version/settings-tab.xhtml"/>
        </li>
      </s:fragment>
    </ul>
  </div>
</div>
</div>

<rich:popupPanel id="uploadSourcePanel"
  moveable="#{false}"
  resizeable="#{false}"
  autosized="#{true}">
  <f:facet name="header">
    <h:outputText id="uploadSourcePanelHeader"/>
  </f:facet>
  <h:form id="uploadSrcForm" enctype="multipart/form-data">
    <s:fileUpload id="specificDocFileInput"
      accept="*"
      onchange="checkIfSpecificFileSelected();"
      data="#{versionHomeAction.sourceFileUpload.fileContents}"
      fileName="#{versionHomeAction.sourceFileUpload.fileName}"/>
    <h:inputHidden id="uploadSourceDocId"
      value="#{versionHomeAction.sourceFileUpload.docId}"/>
    <br/>
    <h:selectOneMenu
      value="#{versionHomeAction.sourceFileUpload.sourceLang}">
      <s:selectItems
        value="#{versionHomeAction.availableSourceLocales}" var="locale"
        label="#{locale.retrieveNativeName()}"
        itemValue="#{locale.localeId.toString()}"
        escape="true"/>
      <h:outputLabel value="#{msgs['jsf.SourceLanguage']}"/>
    </h:selectOneMenu>
    <br/>
    <rich:collapsiblePanel
      header="#{msgs['jsf.iteration.files.CustomParams']}"
      switchType="client"
      expanded="false">
      <h:outputLabel for="adapterParams"
        value="#{msgs['jsf.iteration.files.CustomParams.description']}"/>
      <br/>
      <h:inputTextarea id="adapterParams"
        value="#{versionHomeAction.sourceFileUpload.adapterParams}"/>
      <br/>
      <a target="_blank"
        href="https://github.com/zanata/zanata-server/wiki/Custom-document-parameters">
        #{msgs['jsf.iteration.files.CustomParams.linkText']}
      </a>
    </rich:collapsiblePanel>

    <div style="text-align:right">
      <h:commandButton id="specificDocSubmitUploadButton"
        value="#{msgs['jsf.Upload']}"
        action="#{versionHomeAction.uploadSourceFile}"/>
      <h:commandButton value="#{msgs['jsf.Cancel']}"
        onclick="return hideUploadSourcePanel();"/>
    </div>
  </h:form>
</rich:popupPanel>


<rich:popupPanel id="uploadSourceDocPanel"
  moveable="#{false}"
  resizeable="#{false}"
  autosized="#{true}">
  <f:facet name="header">
    <h:outputText
      value="#{msgs['jsf.iteration.files.UploadNewSourceDocument']}"/>
  </f:facet>
  <h:form id="uploadDocForm" enctype="multipart/form-data">
    <s:fileUpload id="generalDocFileUpload"
      accept="*"
      onchange="checkIfGeneralFileSelected();"
      data="#{versionHomeAction.sourceFileUpload.fileContents}"
      fileName="#{versionHomeAction.sourceFileUpload.fileName}"/>

    <div id="invalidFilenameError" style="display: none;
                                                      color: red;
                                                      padding: 5px;
                                                      margin-bottom: 5px;">
      #{msgs['jsf.iteration.files.FilenameWithSemicolonNotSupported']}
    </div>
    <div>
      <h:outputLabel for="docPath"
        value="#{msgs['jsf.iteration.files.DocumentPath']}"/>
      <h:inputText id="docPath"
        value="#{versionHomeAction.sourceFileUpload.documentPath}">
      </h:inputText>
    </div>
    <div>
      <h:selectOneMenu
        value="#{versionHomeAction.sourceFileUpload.sourceLang}">
        <s:selectItems
          value="#{versionHomeAction.availableSourceLocales}"
          var="locale"
          label="#{locale.retrieveNativeName()}"
          itemValue="#{locale.localeId.toString()}"
          escape="true"/>
        <h:outputLabel value="#{msgs['jsf.SourceLanguage']}"/>
      </h:selectOneMenu>
    </div>
    <rich:collapsiblePanel
      header="#{msgs['jsf.iteration.files.CustomParams']}"
      switchType="client"
      expanded="false">
      <h:outputLabel for="adapterParams"
        value="#{msgs['jsf.iteration.files.CustomParams.description']}"/>
      <br/>
      <h:inputTextarea id="adapterParams"
        value="#{versionHomeAction.sourceFileUpload.adapterParams}"/>
      <br/>
      <a target="_blank"
        href="https://github.com/zanata/zanata-server/wiki/Custom-document-parameters">
        #{msgs['jsf.iteration.files.CustomParams.linkText']}
      </a>
    </rich:collapsiblePanel>

    <div style="text-align:right">
      <h:commandButton id="generalDocSubmitUploadButton"
        value="#{msgs['jsf.Upload']}"
        action="#{versionHomeAction.uploadSourceFile}"/>
      <h:commandButton id="generalDocCancelUploadButton"
        value="#{msgs['jsf.Cancel']}"
        onclick="return hideSourceDocUploadPanel();"/>
    </div>
  </h:form>
</rich:popupPanel>
</ui:define>

</ui:composition>
